package com.webcash.CNBoard.Controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.CalendarModel;
import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.EmpBoard;
import com.webcash.CNBoard.Model.EmpComment;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.PositionName;
import com.webcash.CNBoard.Model.Schedule;
import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Service.BoardService;
import com.webcash.CNBoard.Service.DepartmentService;
import com.webcash.CNBoard.Service.DepartmentTeamService;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Service.PositionNameService;
import com.webcash.CNBoard.Service.ProjectService;
import com.webcash.CNBoard.util.CalendarUtil;



@Controller
@RequestMapping("/teamMember")
@SessionAttributes("login")
public class DepartmentTeamMemberController {
	
	@Autowired
	DepartmentTeamService departmentTeamService;
	
	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	DepartmentService departmentService;
	
	@Autowired
	BoardService boardService;

	@Autowired
	ProjectService projectService;
	
	@Autowired
	PositionNameService positionNameService;
	
	
	
	@RequestMapping("/teamMemberInfo")
	public String TeamMemberInfo(@ModelAttribute Login login, Model model, @ModelAttribute Employee employee) {
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		Department dept = departmentService.getDepartmentByDeptNo(Integer.parseInt(emp.getDeptNo()));
		
		int pageNum = 1;
		List<EmpBoard> boardList = boardService.getEmpBoardListByEmpNo(emp.getEmpNo(), pageNum);
		
		List<Work> workList = projectService.getNewAllWorksById(emp.getId());
		int count = boardService.getEmpBoardCountByEmpNo(emp.getEmpNo());
		PositionName emppos = positionNameService.getPositionNamebyPositionNo(Integer.parseInt(emp.getPosition()));
		
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("boardList", boardList);
		model.addAttribute("write", false);
		model.addAttribute("workList", workList);
		model.addAttribute("emppos", emppos);
		model.addAttribute("boardCount", count);
		model.addAttribute("pageNum", pageNum);
		
		return "department/deptTeamMemberPage";
	}
	
	@RequestMapping("/write") // 글쓰기 페이지 이동
	public String write(Model model, @ModelAttribute Employee employee) {
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		model.addAttribute("emp", emp);
		return "department/deptTeamMemberboardWrite";
	}
	
	@RequestMapping("/modify")
	public String modify(@ModelAttribute Login login, @RequestParam int empBoardNo,  Model model, @ModelAttribute Employee employee){
		
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		EmpBoard empBoard = boardService.getEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		model.addAttribute("emp", emp);
		model.addAttribute("empBoard", empBoard);
		System.out.println(empBoard);
		return "department/deptTeamMemberboardModify";
	}

	@RequestMapping(value = "/writeAction", method=RequestMethod.POST) //글쓰기 ajax
	@ResponseBody
	public String writeAction(@ModelAttribute Login login, @ModelAttribute Board board,  Model model, @ModelAttribute Employee employee) {
		
		String data = "fail"; //or success 로 구분해서 ajax 컨트롤 유효성 검사하고 title이나 article 비어있는지
		
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		boardService.addEmpBoardByEmpNo(login.getId(), board, emp.getEmpNo());
		model.addAttribute("emp", emp);
		return data;
	}
	
	@RequestMapping(value = "/modifyAction") //글쓰기 ajax
	@ResponseBody
	public String modifyAction(@ModelAttribute Login login, @ModelAttribute Board board, @RequestParam int empBoardNo, Model model, @ModelAttribute Employee employee) {
		
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		boardService.updateEmpBoardByBoardNo(board, empBoardNo, emp.getEmpNo());
		return "";
	}
	@RequestMapping(value = "/list") // 게시물 리스트
	public String list(@ModelAttribute Login login, @RequestParam(required=false) String num, Model model, @ModelAttribute Employee employee) {
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		
		Department dept = departmentService.getDepartmentByDeptNo(Integer.parseInt(emp.getDeptNo()));
		int count = boardService.getEmpBoardCountByEmpNo(emp.getEmpNo());
		int pageNum = 1;
		if(num == null)
			System.out.println("num is null");
		else{
			System.out.println(num);
			pageNum = Integer.parseInt(num);
			
		}
		
		List<EmpBoard> boardList = boardService.getEmpBoardListByEmpNo(emp
				.getEmpNo(), pageNum);
		
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("boardList", boardList);
		model.addAttribute("boardCount", count);
		model.addAttribute("pageNum", pageNum);
		System.out.println(emp);
		
		return "department/deptTeamMemberboard";
	}
	
	@RequestMapping(value = "/list/next")
	public String nextList(){
		
		return "";
	}
	
	
	@RequestMapping(value="/contents/{empBoardNo}") //게시물 선택
	public String listAction(@PathVariable int empBoardNo, @RequestParam int num, @RequestParam(required=false) boolean updateComment, @ModelAttribute Login login, Model model, @ModelAttribute Employee employee){
		
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		System.out.println(updateComment);
		if(!updateComment)
			boardService.updateEmpBoardForReadCountByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		EmpBoard empBoard = boardService.getEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		List<EmpComment> commentList = boardService.getEmpCommentListByBoardNo(empBoard.getBoardNo());
		
		model.addAttribute("emp", emp);
		model.addAttribute("empBoard", empBoard);
		model.addAttribute("pageNum", num);
		model.addAttribute("commentList", commentList);
		return "department/deptTeamMemberboardContents";
	}
	
	@RequestMapping(value="/delete/{empBoardNo}") //게시판 삭제
	public String deleteAction(@PathVariable int empBoardNo, @ModelAttribute Login login, Model model, @ModelAttribute Employee employee){
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		boardService.removeEmpBoardByEmpNoAndEmpBoardNo(emp.getEmpNo(), empBoardNo);
		model.addAttribute("emp", emp);
		return "department/deptTeamMemberboard";
	}
	
	@RequestMapping(value="/progress") //업무 progress 비동기
	public String getProgressWorks(@ModelAttribute Login login, Model model, @ModelAttribute Employee employee){
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		List<Work> workList = projectService.getProgressWorkListById(emp.getId());
		model.addAttribute("emp", emp);
		model.addAttribute("workList", workList);
		return "department/deptTeamMemberWorksContent";
	}
	@RequestMapping(value="/order") //업무 order 비동기 
	public String getOrderWorks(@ModelAttribute Login login, Model model, @ModelAttribute Employee employee){
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		List<Work> workList = projectService.getNewAllWorksById(emp.getId());
		model.addAttribute("emp", emp);
		model.addAttribute("workList", workList);
		return "department/deptTeamMemberWorksContent";
	}
	
	@RequestMapping(value="/schedule")
	public String getSchedule(@ModelAttribute Login login, Model model, @ModelAttribute Employee employee){
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		CalendarUtil calUtil = new CalendarUtil();
		List<Schedule> scList = employeeService.getScheduleListByEmpNo(emp.getEmpNo());
		
		CalendarModel cal = calUtil.calendar();
		List<Work> workList = projectService.getAllWorkListById(emp.getId());
		model.addAttribute("emp", emp);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("scList", scList);
		return "department/deptTeamMemberschedule";
	}
	
	@RequestMapping(value="/schedule/changeMonth")
	public String changeSchedule(@ModelAttribute Login login,@RequestParam int year, @RequestParam int month, Model model, @ModelAttribute Employee employee){
		Employee emp = employeeService.getEmployeebyEmpNo(employee.getEmpNo());
		
		CalendarUtil calUtil = new CalendarUtil(year, month);
		
		CalendarModel cal = calUtil.calendar();
		List<Work> workList = projectService.getAllWorkListById(emp.getId());
		
		model.addAttribute("emp", emp);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
				
		return "department/deptTeamMemberschedule";
	}
	
	@RequestMapping(value="/comment/write")
	@ResponseBody
	public String commentWrite(@ModelAttribute Login login,@RequestParam int boardNo, @RequestParam String commentContents,  Model model, @ModelAttribute Employee employee){
		System.out.println(commentContents +"/" + boardNo);
		
		boardService.addEmpCommentByBoardNoAndId(boardNo, login.getId(), commentContents);
		
		return "";
	}
	
	
}
